﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarAgeCapacityDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarAgeCapacityDB
    {
        public CarAgeCapacityDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarAgeCapacity caragecapacity)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarAgeCapacityInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCarAgeCapId", caragecapacity.ProviderCarAgeCapId);
            _sPara.Add("@ProviderCompanyId", caragecapacity.ProviderCompanyId);
            _sPara.Add("@Age", caragecapacity.CarAge);
            _sPara.Add("@Capacity", caragecapacity.CarCapacity);
            _sPara.Add("@Rate", caragecapacity.Rate);
            _sPara.Add("@RefCEUserId", caragecapacity.RefCEUserId);
            _sPara.Add("@Status", caragecapacity.Status == true ? 1 : 0);
            _sPara.Add("@Finfo1", caragecapacity.FInfo1);
            _sPara.Add("@Dinfo1", caragecapacity.DInfo1);

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public DataTable GetCarAgeCapacityList(Int64 ProviderCompanyId, Int64 ProviderCarAgeCapId)
        {
            DataTable _caragecapacitylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarAgeCapacitySelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ProviderCarAgeCapId", ProviderCarAgeCapId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caragecapacitylist = _dt;
                }
            }
            return _caragecapacitylist;
        }

        public DataTable GetCarAgeCapacityList(Int64 ProviderCompanyId)
        {
            DataTable _caragecapacitylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarAgeCapacitySelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caragecapacitylist = _dt;
                }
            }
            return _caragecapacitylist;
        }

        public CarAgeCapacity GetCarAgeCapacity(Int64 ProviderCarAgeCapId)
        {
            CarAgeCapacity _caragecapacity = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarAgeCapacitySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarAgeCapId", ProviderCarAgeCapId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caragecapacity = new CarAgeCapacity();
                    _caragecapacity = FillDataRecord(_dt, 0);
                }
            }
            return _caragecapacity;
        }

        public CarAgeCapacity GetCarAgeCapacity(Int64 ProviderCompanyId, string CarAge, string Capacity)
        {
            CarAgeCapacity _caragecapacity = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarAgeCapacityItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@Age", CarAge);
            _spara.Add("@Capacity", Capacity);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caragecapacity = new CarAgeCapacity();
                    _caragecapacity = FillDataRecord(_dt, 0);
                }
            }
            return _caragecapacity;
        }

        private CarAgeCapacity FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarAgeCapacity myCarAgeCapacity = new CarAgeCapacity();
            myCarAgeCapacity.ProviderCarAgeCapId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarAgeCapId"].ToString());
            myCarAgeCapacity.ProviderCompanyId = Convert.ToInt32(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarAgeCapacity.CarAge = myDataTable.Rows[RowId]["Age"].ToString();
            myCarAgeCapacity.CarCapacity = myDataTable.Rows[RowId]["Capacity"].ToString();
            myCarAgeCapacity.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarAgeCapacity.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarAgeCapacity.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarAgeCapacity.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarAgeCapacity.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarAgeCapacity.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarAgeCapacity;
        }

    }
}